package cn.jaye.fabric.controller;

import cn.jaye.fabric.application.ApiPackageApplicationService;
import cn.jaye.fabric.application.command.ListApiPackageCommand;
import cn.jaye.fabric.common.constant.Global;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.jaye.fabric.application.command.ChangeApiPackageCommand;
import cn.jaye.fabric.application.command.CreateApiPackageCommand;
import cn.jaye.fabric.common.constant.ExceptionEnum;
import cn.jaye.fabric.common.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jaye
 * @create 2017-12-29 15:12
 */
@RestController
@RequestMapping("/apipackage")
public class ApiPackageController {

    @Autowired
    private ApiPackageApplicationService apiPackageApplicationService;

    @PostMapping("/list")
    public String listsApiPackage(HttpServletRequest request) {
        JSONObject requstParam = JSON.parseObject(request.getParameter("aodata"));
        int sEcho = requstParam.getIntValue("sEcho");
        String sSearch = requstParam.getString("sSearch");//查询条件
        Integer start = requstParam.getInteger("iDisplayStart");
        Integer length = requstParam.getInteger("iDisplayLength");
        Integer currentPage;
        currentPage = (start / length) + 1;
        Map<String, Object> map = new HashMap<>();
        map.put("aaData", new ArrayList<>());
        map.put("iTotalDisplayRecords", 0);
        map.put("iTotalRecords", 0);
        map.put("sEcho", sEcho + 1);
        ListApiPackageCommand listApiPackageCommand = new ListApiPackageCommand();
        listApiPackageCommand.setSizes(length);
        listApiPackageCommand.setCurrentPage(currentPage);
        if (StringUtils.isNotBlank(sSearch)) {
            listApiPackageCommand.setCondition(sSearch);
        }
        String data = apiPackageApplicationService.listApiPackage(listApiPackageCommand);
        JSONObject jsonObject = JSON.parseObject(data);
        if (Global.SUCCESS_FLAG.equals(jsonObject.getInteger("code"))) {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("records");
            if (jsonArray.size() > 0) {
                List<Map> r = jsonArray.toJavaList(Map.class);
                map.put("aaData", r);
                map.put("iTotalDisplayRecords", jsonObject.getJSONObject("data").getInteger("total"));
                map.put("iTotalRecords", jsonObject.getJSONObject("data").getInteger("total"));
                map.put("sEcho", sEcho + 1);
            }
        }
        return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
    }

    @PostMapping("/list/all")
    public String listAllApiPackage(HttpServletRequest request) {
        return apiPackageApplicationService.listApiPackageAll();
    }

    @PostMapping("/create")
    public String createApiPackage(HttpServletRequest request, @ModelAttribute CreateApiPackageCommand createApiPackageCommand) {
        return apiPackageApplicationService.createApiPackage(createApiPackageCommand);
    }

    @PostMapping("/get")
    public String getApiPackage(HttpServletRequest request, @RequestBody String data) {
        return apiPackageApplicationService.getApiPackage(JSON.parseObject(data).getString("apiPackageId"));
    }

    @PostMapping("/change/save")
    public String saveChangeApiPackage(HttpServletRequest request, @RequestBody String data) {
        ChangeApiPackageCommand changeApiPackageCommand = JSON.parseObject(data, ChangeApiPackageCommand.class);
        if (apiPackageApplicationService.saveChange(changeApiPackageCommand)) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(ExceptionEnum.APIPACKAGE_SETTING_FAILED);
    }
}
